#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

import math
import numpy
from qtcam.core.parameters import SectionParameters


class FifthDegreePolynomialSection(SectionParameters):  # Need inheritance

    def __init__(self, LowestPosition=0.0,
                 HighestPosition=50.0, StartAngle=0.0,
                 EndAngle=100.0, Precision=0.1, AngularVelocity=1):
        "Initializes all necessary paramaters for further calculation"
        super(FifthDegreePolynomialSection, self).__init__(LowestPosition,
                                                           HighestPosition,
                                                           StartAngle,
                                                           EndAngle,
                                                           Precision,
                                                           AngularVelocity)

    def Position(self, currentAngle):
        """Returns the Position corresponding to a Polinomial
        section of a cam after an angle in degrees is given

        Keyword Arguments:
        currentAngle --
        """
        return (self.LowestPosition +
                self.HighestPosition * ((10.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 3)) -
                                        (15.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 4)) -
                                        (6.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 5))))

    def Velocity(self, currentAngle):
        """Returns the angular velocity corresponding
        to a polinomial section of a cam after an angle is given
        Keyword Arguments:
        currentAngle --
        """
        return (self.AngularVelocity *
                self.HighestPosition * ((30.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 2)) -
                                        (60.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 3)) -
                                        (30.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 4))))

    def Acceleration(self, currentAngle):
        """Returns the acceleration of the cam after an angle  is given


        Keyword Arguments:
        currentAngle --
        """
        return (math.pow(self.AngularVelocity, 2) *
                self.HighestPosition * ((60.0 *
                                         (currentAngle /
                                          self.EndAngle, 2)) -
                                        (180.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 2)) -
                                        (120.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 3))))

    def OverAcceleration(self, currentAngle):  # consultar al profesor
        """Returns the derivative of the acceleration after an
        angle is given as an argument

        Keyword Arguments:
        currentAngle --
        """
        return (math.pow(self.AngularVelocity, 3) *
                self.HighestPosition * ((30.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 2)) -
                                        (60.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 3)) -
                                        (30.0 *
                                         math.pow(currentAngle /
                                                  self.EndAngle, 4))))

    def ArrayPosition(self):
        """Creates an array with a all positions in the section

        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (self.LowestPosition +
                self.HighestPosition * ((10.0 *
                                         numpy.power((theta /
                                                     beta), 3)) -
                                        (15.0 *
                                         numpy.power((theta /
                                                     beta), 4)) +
                                        (6.0 *
                                         numpy.power((theta /
                                                      beta), 5))))

    def ArrayVelocity(self):
        """Creates an array with all Velocities in the section
        
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return ((self.AngularVelocity / math.radians(beta)) *
                self.HighestPosition * ((30.0 *
                                         numpy.power((theta / beta), 2)) -
                                        (60.0 *
                                         numpy.power((theta / beta), 3)) +
                                        (30.0 *
                                         numpy.power((theta / beta), 4))))

    def ArrayAcceleration(self):
        """Creates an array with all the acceleration for the angle
        values closed by the interval 
        defined by StartAngle and EndAngle
        
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (numpy.power((self.AngularVelocity / math.radians(beta)), 2) *
                self.HighestPosition * ((60.0 *
                                         (theta / beta)) -
                                        (180.0 *
                                         numpy.power((theta / beta), 2)) +
                                        (120.0 *
                                         numpy.power((theta / beta), 3))))

    def ArrayOverAcceleration(self):  # Vigila este
        """Creates an array with all values of the derivative
        of the acceleration from the interval defined by StartAngle
        and EndAngle and the precision given by Precision.
        
        """
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (numpy.power((self.AngularVelocity / math.radians(beta)), 3) *
                self.HighestPosition * (60.0 -
                                        (360.0 *
                                         (theta / beta)) +
                                        (360.0 *
                                         numpy.power((theta /
                                                      beta), 2))))
